#   Trolls Outta Luckland v0.0.6
#   Copyright 2009-2010, Gummbum
#
#   This file is part of Trolls Outta Luckland.
#
#   Trolls Outta Luckland is free software: you can redistribute it
#   and/or modify it under the terms of the GNU General Public License
#   as published by the Free Software Foundation, either version 3 of
#   the License, or (at your option) any later version.
#
#   Trolls Outta Luckland is distributed in the hope that it will be
#   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
#   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with Trolls Outta Luckland.  If not, see
#   <http://www.gnu.org/licenses/>.

__version__ = '0.0.4'
__revision__ = '0.0.1'

### Python imports
import os
import sys

try:
    ### Pygame imports
    import pygame
    from pygame.locals import Color
    ### Game library imports
    import game_globals
    from game_globals import *
    import settings
    from utils import fill_gradient
except ImportError, e:
    print 'ImportError: %s' % e
    print '    in file "%s"' % os.path.abspath(sys.argv[0])

desktop_rect = pygame.Rect(0,0,0,0)
window_rect = pygame.Rect(0,0,0,0)

def _choose_displays():
    max_ = Display(0,0)
    req = Display(WINDOW_WIDTH,WINDOW_HEIGHT)
    pref_full = Display(WINDOW_WIDTH,WINDOW_HEIGHT)
    pref_win = Display(WINDOW_WIDTH+48,WINDOW_HEIGHT+12)
    fullscreen = Display(0,0, FULLSCREEN)
    windowed = Display(pref_win.size)
    for mode in pygame.display.list_modes(0, FULLSCREEN):
        screen_mode = Display(mode)
        if screen_mode > max_:
            max_.size = screen_mode.size
        if screen_mode >= pref_full:
            fullscreen.size = screen_mode.size
    if fullscreen < req:
        raise pygame.error, 'minimum display %s not supported' % (req.size,)
    if windowed.height == max_.height:
        # remove frame to prevent accidental repositoning
        windowed.flags = NOFRAME
    return fullscreen,windowed

class Display():
    def __init__(self, w, h=None, flags=0):
        # self.size is a virtual attribute equivalent to (w,h)
        if h is None:
            w,h = w
        self.width = w
        self.height = h
        self.flags = flags
    def __getattr__(self, name):
        if name == 'size':
            return self.width,self.height
    def __setattr__(self, name, val):
        if name == 'size':
            self.__dict__['width'],self.__dict__['height'] = val
        else:
            self.__dict__[name] = val
    def __str__(self):
        return repr(self.size)
    def __equal__(self, other):
        return self.width == other.width and self.height == other.height
    def __le__(self, other):
        return self.width <= other.width and self.height <= other.height
    def __lt__(self, other):
        return self.width < other.width and self.height < other.height
    def __ge__(self, other):
        return self.width >= other.width and self.height >= other.height
    def __gt__(self, other):
        return self.width > other.width and self.height > other.height

class Window(object):
    def __init__(self, color=BORDER_COLOR, gradient=BORDER_GRADIENT):
        global desktop_rect, window_rect
        self.color = color
        self.gradient = gradient
        self.fullscreen,self.windowed = _choose_displays()
        wininfo = pygame.display.Info()
        desktop_rect.size = wininfo.current_w,wininfo.current_h
        window_rect.size = self.windowed.size
        window_rect.center = desktop_rect.center
        os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % window_rect.topleft
        if settings.fullscreen:
            self.set_fullscreen(new_bg=False)
        else:
            self.set_windowed(new_bg=False)
        pygame.mouse.set_visible(settings.visible_mouse)
        self.display_rect = self._surface.get_rect()
        pygame.display.set_caption('Trolls Outta Luckland '+__version__)
        # game drawing area
        self._subrect = pygame.Rect(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT)
        self._subrect.centerx = self.display_rect.centerx
        self.surface = self._surface.subsurface(self._subrect)
        self.rect = self.surface.get_rect()
        # background management
        self.new_background()
    def clear(self):
        self._surface.blit(self.bg_image, self._subrect)
    def flip(self):
        pygame.display.flip()
    def new_background(self, color=BORDER_COLOR, gradient=BORDER_GRADIENT):
        if type(color) is str:
            color = Color(color)
        elif type(color) is tuple:
            color = Color(*color)
        if type(gradient) is str:
            gradient = Color(gradient)
        elif type(color) is tuple:
            gradient = Color(*gradient)
        fill_gradient(self._surface, color, gradient)
        self.bg_image = pygame.surface.Surface((WINDOW_WIDTH,WINDOW_HEIGHT))
        self.bg_image.convert()
        self.bg_image.fill(Color('black'), self._subrect)
    def is_fullscreen(self):
        return self._surface.get_flags() & FULLSCREEN == FULLSCREEN
    def toggle_fullscreen(self):
        if self.is_fullscreen():
            self.set_windowed()
        else:
            self.set_fullscreen()
        self.new_background()
    def set_mode(self, flags):
        if flags & FULLSCREEN:
            return self.set_fullscreen()
        else:
            return self.set_windowed()
    def set_windowed(self, new_bg=True):
        self._surface = pygame.display.set_mode(self.windowed.size, self.windowed.flags)
        if new_bg:
            self.display_rect = self._surface.get_rect()
            self._subrect.centerx = self.windowed.width / 2
            self.surface = self._surface.subsurface(self._subrect)
            self.rect = self.surface.get_rect()
            self.new_background()
        return self._surface
    def set_fullscreen(self, new_bg=True):
        self._surface = pygame.display.set_mode(
            self.fullscreen.size, self.fullscreen.flags)
        if new_bg:
            self.display_rect = self._surface.get_rect()
            self._subrect.centerx = self.display_rect.centerx
            self.surface = self._surface.subsurface(self._subrect)
            self.rect = self.surface.get_rect()
            self.new_background()
        return self._surface

if __name__ == '__main__':
    pygame.init()
    win = Window()
    pygame.time.wait(3000)
